package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.MesMessage;
import com.ruoyi.common.core.domain.entity.SysUserFriend;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.mapper.SysUserFriendMapper;
import com.ruoyi.system.service.ISysUserFriendService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysUserFriendServiceImpl implements ISysUserFriendService {


    @Resource
    private SysUserFriendMapper userFriendMapper;
    @Resource
    private RedisCache redisCache;

    @Resource
    private RedisTemplate redisTemplate;


    @Override
    public List<SysUserFriend> selectUserFriendList(int userId) {
        //获取好友列表
        List<SysUserFriend> sysUserFriendMappers = userFriendMapper.selectUserFriendList(userId);
        //获取最后一条消息的列表
        List<MesMessage> messageList = userFriendMapper.getListMessageList(userId);
         Map<String,String> messageMap= new HashMap<>();
         //将最后一条数据转移到map中
        for (MesMessage mesMessage : messageList) {
            messageMap.put(String.valueOf(mesMessage.getFriendId()),mesMessage.getMessage());
        }
        //遍历好友列表 将最后一条消息添加到对象中
        for (SysUserFriend sysUserFriendMapper : sysUserFriendMappers) {
            sysUserFriendMapper.setLastMessage(messageMap.get(String.valueOf(sysUserFriendMapper.getFriendId())));
        }
        return sysUserFriendMappers;
    }

    @Override
    public int insertHistory(MesMessage message) {
        message.setSendTime(new Date());
        int i = userFriendMapper.insertHistory(message);
        return i;
    }

    @Override
    public List<MesMessage>  selectMessageById(int userId, int friendId) {
        //分页查询
        //先查询缓存
        Object o = redisTemplate.opsForHash().get(String.valueOf(userId), String.valueOf(friendId));
        if (o!=null){
            //存在，则直接获取缓存数据
            return (List<MesMessage>)o;
        }
        //不存在，则查询数据库
        List<MesMessage> mesMessages = userFriendMapper.selectMessageById(userId,friendId);
        redisTemplate.opsForHash().put(String.valueOf(userId), String.valueOf(friendId),mesMessages);
        return mesMessages;
    }

    @Override
    public List<MesMessage> getAllMessageById(int userId) {
        List<MesMessage> allMessageById = userFriendMapper.getAllMessageById(userId);
        List<SysUserFriend> friendIdList = userFriendMapper.selectUserFriendId(userId);
        Map<String,String>  map = new HashMap<>();
        for (SysUserFriend userFriend : friendIdList) {
            for (MesMessage mesMessage : allMessageById) {

            }

            redisTemplate.opsForHash().put(userId, userFriend.getFriendId(),map);

        }



        return null;
    }

    @Override
    public List<MesMessage> getListMessageList(int userId) {
        return null;
    }
}
